import java.io.*;
import java.util.*;

class Team {
    String name; // 队名
    int matchesPlayed; // 已比赛的场数
    int wins; // 赢的场数
    int draws; // 平的场数
    int losses; // 负的场数
    int goalsFor; // 进球数
    int goalsAgainst; // 失球数
    int points; // 积分

    public Team(String name) {
        this.name = name;
        this.matchesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    public void updateStats(int scored, int conceded) {
        matchesPlayed++;
        goalsFor += scored;
        goalsAgainst += conceded;

        if (scored > conceded) {
            wins++;
            points += 3;
        } else if (scored == conceded) {
            draws++;
            points += 1;
        } else {
            losses++;
        }
    }

    @Override
    public String toString() {
        return String.format("%-15s %2d %2d %2d %2d %2d %2d %2d",
                name, matchesPlayed, wins, draws, losses, goalsFor, goalsAgainst, points);
    }
}

public class LeagueTable {
    private static List<Team> teams = new ArrayList<>();

    public static void main(String[] args) {
        loadTeamsFromFile("teams.txt");
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入比赛结果（格式：主队 客队 主队得分 客队得分），输入exit退出：");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("exit")) {
                break;
            }

            String[] parts = input.split(" ");
            if (parts.length != 4) {
                System.out.println("输入格式错误，请重新输入。");
                continue;
            }

            String homeTeamName = parts[0];
            String awayTeamName = parts[1];
            int homeScore = Integer.parseInt(parts[2]);
            int awayScore = Integer.parseInt(parts[3]);

            Team homeTeam = findTeam(homeTeamName);
            Team awayTeam = findTeam(awayTeamName);

            if (homeTeam != null && awayTeam != null) {
                homeTeam.updateStats(homeScore, awayScore);
                awayTeam.updateStats(awayScore, homeScore);
                System.out.println("更新成功！");
            } else {
                System.out.println("找不到球队，请检查输入的队名。");
            }

            sortTeams();
            displayTable();
            saveTeamsToFile("teams.txt");
        }

        scanner.close();
    }

    private static void loadTeamsFromFile(String filename) {
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                teams.add(new Team(line.trim()));
            }
        } catch (IOException e) {
            System.out.println("读取文件出错: " + e.getMessage());
        }
    }

    private static void saveTeamsToFile(String filename) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
            for (Team team : teams) {
                bw.write(team.name);
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("写入文件出错: " + e.getMessage());
        }
    }

    private static Team findTeam(String name) {
        for (Team team : teams) {
            if (team.name.equalsIgnoreCase(name)) {
                return team;
            }
        }
        return null;
    }

    private static void sortTeams() {
        teams.sort((t1, t2) -> {
            if (t1.points != t2.points) {
                return Integer.compare(t2.points, t1.points);
            }
            int goalDifference1 = t1.goalsFor - t1.goalsAgainst;
            int goalDifference2 = t2.goalsFor - t2.goalsAgainst;
            if (goalDifference1 != goalDifference2) {
                return Integer.compare(goalDifference2, goalDifference1);
            }
            return Integer.compare(t2.goalsFor, t1.goalsFor);
        });
    }

    private static void displayTable() {
        System.out.println("球队名          场数 胜 平 负 进球 失球 积分");
        for (Team team : teams) {
            System.out.println(team);
        }
    }
}

